<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/16
 * Time: 10:52
 */

namespace App\HttpController\Home;

use App\Enums\UrlEnums;
use App\Library\Auth;
use App\Service\LiveService;
use App\Utility\Pool\MysqlObject;
use App\Utility\Pool\MysqlPool;
use EasySwoole\HttpClient\HttpClient;

class Live extends Base
{

    public function lists()
    {
        $liveService = new LiveService();
        $lol = $liveService->lol();
        foreach ( $lol as $k1=>$v1 ){
            $lol[$k1]['game'] = 'lol';
        }
        $dota = $liveService->dota();
        foreach ( $dota as $k2=>$v2 ){
            $dota[$k2]['game'] = 'dota';
        }
        $csgo = $liveService->csgo();
        foreach ( $csgo as $k3=>$v3 ){
            $csgo[$k3]['game'] = 'csgo';
        }
        $kog = $liveService->kog();
        foreach ( $kog as $k4=>$v4 ){
            $csgo[$k3]['game'] = 'kog';
        }

        $merge_one = array_merge_recursive($lol,$dota);
        $merge_two = array_merge_recursive($merge_one,$csgo);
        $merge_three = array_merge_recursive($merge_two,$kog);
        $count = count($merge_three);
        for ( $i=0;$i<$count;$i++ ){
            $isSorted = true;
            $sortBorder = $count-1;
            for ( $j=0;$j < $sortBorder;$j++){
                if( $merge_three[$j]['match_time'] > $merge_three[$j+1]['match_time'] ){
                    $merge_three[$j] = $merge_three[$j+1];
                    $merge_three[$j+1] = $merge_three[$j];
                    $isSorted = false;
                    $sortBorder = $j;
                }
            }
            if( $isSorted ){
                break;
            }
        }
        return $this->writeJson(1,$merge_three,'请求成功');
    }

    /**
     * 正在比赛的列表
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function lolLists()
    {
        //正在进行的比赛
        $liveService = new LiveService();
        $matchs = $liveService->lol();
        return $this->writeJson(1,$matchs,'请求成功');
    }

    /**
     * 正在进行的比赛列表
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function dotaLists()
    {
        //正在进行的比赛
        $liveService = new LiveService();
        $matchs = $liveService->dota();
        return $this->writeJson(1,$matchs,'请求成功');
    }

    /**
     * csgo正在进行的比赛
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function csgoList()
    {
        $liveService = new LiveService();
        $lists = $liveService->csgo();
        return $this->writeJson(1,$lists,'请求成功');
    }

    /**
     * kog正在进行的比赛
     * @return bool
     * @throws \EasySwoole\Component\Pool\Exception\PoolEmpty
     * @throws \EasySwoole\Component\Pool\Exception\PoolException
     * @throws \Throwable
     */
    public function kogList()
    {
        $liveService = new LiveService();
        $lists = $liveService->kog();
        return $this->writeJson(1,$lists,'请求成功');
    }

}